package com.dailyblue.example.project.bookstore.orderitem.listener;

import com.alibaba.fastjson.JSONArray;
import com.dailyblue.example.project.bookstore.commons.bean.OrderItem;
import com.dailyblue.example.project.bookstore.orderitem.service.OrderItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "order_item_group", topic = "ORDER2", selectorExpression = "ORDER_ITEM", messageModel = MessageModel.BROADCASTING)
public class OrderListener implements RocketMQListener<String> {
    @Resource
    private OrderItemService service;

    @Override
    public void onMessage(String s) {
        List<OrderItem> list = JSONArray.parseArray(s, OrderItem.class);
        log.info("list:{}", list);
        service.saveBatch(list);
    }
}
